package com.bgrconsulting.stockorderentry.service.rest.impl;

import java.util.Date;
import java.util.List;
import java.util.Locale;

import com.bgrconsulting.stockorderentry.service.model.Message;
import com.bgrconsulting.stockorderentry.service.model.action.PreviewEquityOrder;
import com.bgrconsulting.stockorderentry.service.model.response.EquityOrderResponse;
import com.bgrconsulting.stockorderentry.service.model.response.PreviewEquityOrderResponse;

import com.bgrconsulting.stockorderentry.service.converter.PreviewEquityOrderConverter;


import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.CacheControl;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.EntityTag;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.NewCookie;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Variant;
import javax.xml.bind.JAXBElement;

import com.bgrconsulting.stockorderentry.service.converter.response.PlaceEquityOrderResponseConverter;
import com.bgrconsulting.stockorderentry.service.converter.response.PreviewEquityOrderResponseConverter;
import com.bgrconsulting.stockorderentry.service.dao.PlaceEquityOrderResponseDao;
import com.bgrconsulting.stockorderentry.service.dao.PreviewEquityOrderResponseDao;

import com.sun.org.apache.xerces.internal.util.URI;


import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Request;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.xml.bind.JAXBElement;



@Path("/order/rest/previewequityorder/")
public class PreviewEquityOrderResource {
	@Context
	UriInfo uriInfo;
	@Context
	Request request;
	String id;
	/*public PreviewEquityOrderResource(UriInfo uriInfo, Request request, String id) {
		this.uriInfo = uriInfo;
		this.request = request;
		this.id = id;
	}*/
	
	// Retrieve the Preview Equity Order Response and return it to the requestor. 		
	@GET
	@Path("/{orderId}") 
	@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
	public PreviewEquityOrderResponseConverter getPreviewEquityOrderResponse(@PathParam ("orderId") String orderId) {
		PreviewEquityOrderResponseConverter previewEquityOrderReqponse = PreviewEquityOrderResponseDao.instance.getModel().get(orderId);
		if(previewEquityOrderReqponse==null)
			throw new RuntimeException("Get: Preview equity order with " + orderId +  " not found");
		return previewEquityOrderReqponse;
	}

	@POST
    @Consumes({"application/xml","application/json"})
    public Response createNewPreviewEquityOrder(JAXBElement<PreviewEquityOrderConverter> previewEquityOrder) {
        
		
		PreviewEquityOrderResponse previewEquityOrderResponse = new PreviewEquityOrderResponse(new EquityOrderResponse());
		
		previewEquityOrderResponse.getEquityOrderResponse().setAccountId(previewEquityOrder.getValue().getEquityOrderRequestConverter().getAccountId());
		previewEquityOrderResponse.getEquityOrderResponse().setAllOrNone("false");
		previewEquityOrderResponse.getEquityOrderResponse().setEstimatedCommission(7.99);
		previewEquityOrderResponse.getEquityOrderResponse().setEstimatedTotalAmount(795.95);
		previewEquityOrderResponse.getEquityOrderResponse().setLimitPrice(0.00);
		Message message = new Message("You have an existing open order for this security on the same side of the market. If you did not intend to place a second order for this security, please modify your order now.", "1042");
		previewEquityOrderResponse.getEquityOrderResponse().setMessage(message);
		previewEquityOrderResponse.getEquityOrderResponse().setOrderAction("BUY");
		previewEquityOrderResponse.getEquityOrderResponse().setOrderTerm("GOOD_FOR_DAY");
		previewEquityOrderResponse.getEquityOrderResponse().setOrdertime(new Date());
		previewEquityOrderResponse.getEquityOrderResponse().setOrderNum("449548380022");
		previewEquityOrderResponse.getEquityOrderResponse().setPriceType("STOP");
		previewEquityOrderResponse.getEquityOrderResponse().setQuanity(100);
		previewEquityOrderResponse.getEquityOrderResponse().setReserveOrder(false);
		previewEquityOrderResponse.getEquityOrderResponse().setReserveQuanity(100);
		previewEquityOrderResponse.getEquityOrderResponse().setRoutingDestination("");
			
		PreviewEquityOrderResponseConverter peorc = new PreviewEquityOrderResponseConverter(previewEquityOrderResponse);
		
		return putAndGetResponse(peorc);
		
    }
	
	private Response putAndGetResponse(PreviewEquityOrderResponseConverter previewEquityOrderResponse) {
		Response res;
		if(PreviewEquityOrderResponseDao.instance.getModel().containsKey(previewEquityOrderResponse.getEquityOrderResponse().getPreviewId())) {
			res = Response.noContent().build();
		} else {
			res = Response.created(uriInfo.getAbsolutePath()).build();
		}
		PreviewEquityOrderResponseDao.instance.getModel().put(previewEquityOrderResponse.getEquityOrderResponse().getPreviewId(), previewEquityOrderResponse);
		return res;
	}
}
